// last refactor - 0.2.4

package core.file;

import enviro.Dungeon;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import rougehack.RougeHack;

/**
 * The FileBroker is an application-specific file I/O interface.  Anything
 * that has to do with saving or loading a file is accessed through this API.
 * 
 * @author timon
 */
public class FileBroker {
    
        // Test Message
      
	// <editor-fold defaultstate="collapsed" desc="Member Variables">
	private static FileReader reader = new FileReader();
	// </editor-fold>
        
        //<editor-fold defaultstate="collapsed" desc="public static Image readImage(String)">
        /** 
         * Reads an image from a file and returns it as an Image object.
         * 
         * @param filename
         *   The name of the file to be read.
         * 
         * @return
         *   The Image that was read from the file
         * 
         * @throws IOException 
         *   Thrown if the read operation was unsuccessful.
         */
	public static Image readImage(String filename) throws IOException {
	
		// Acquire buffered image from reader
		BufferedImage bi = reader.readImage(filename);
		
		// Scale image to suit and send back to caller
	    Image img = bi.getScaledInstance(RougeHack.getGridWidth() - 1, 
	    		RougeHack.getGridHeight() - 1, Image.SCALE_FAST);
	    
	    return img;
	    
	}
        // </editor-fold>
        
        //<editor-fold defaultstate="collapsed" desc="public static void saveDungeon(Dungeon dungeon, String path)">
        /**
         * Saves a dungeon to a given filepath.
         * 
         * @param dungeon
         *   The dungeon instance to save
         * @param path 
         *   The path and filename you intend to write into.
         * 
         * @throws FileNotFoundException
         *   Throws when specified file cannot be found
         * @throws IOException
         *   Thrown when operation fails due to an I/O error.
         */
        public static void saveDungeon(Dungeon dungeon, String path) 
                throws FileNotFoundException, IOException {
            
            FileWriter.saveDungeon(dungeon, path);
        }
        //</editor-fold>
        
        // <editor-fold defaultstate="collapsed" desc="public static Dungeon loadDungeon(String path)">
        /**
         * Reads a .dng file and returns the dungeon contents.
         * 
         * @param path
         *   The path and filename where the dungeon is stored
         * 
         * @return
         *   The dungeon object read from the file.
         */
        public static Dungeon loadDungeon(String path) throws 
                FileNotFoundException, IOException, ClassNotFoundException {
            
            // Get the generic object from the file
            Object obj = FileReader.readObject(path);
            
            // Cast the object as a dungeon
            Dungeon dungeon = (Dungeon) obj;
            
            // Return dungeon back to caller
            return dungeon;
            
        }
        //</editor-fold>
}
